﻿// *---------------------------------------------------------------------------------------------------------*
// Solution:         JwikIDE									
// Project:           JwiKIDE								
// Source File:    ConfigFile.cs									
// *---------------------------------------------------------------------------------------------------------*
// 
// © 2012 Digital Six Labs, LLC All Rights Reserved
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// 
// 
// 
namespace JwiKIDE.JavaClassFileClasses
{
    using System.Collections.Generic;
    using System.IO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class ConfigFile
    {
        public static List<string> NativeFileNames { get; set; }

        public static int MaxSize { get; set; }

        public static string TargetFilename { get; set; }

        public static string ConfigName { get; private set; }

        public static List<NativeFile> NativeFiles { get; private set; }

        public static string NativePath { get; set; }

        static ConfigFile()
        {
            ConfigName = string.Empty;
            TargetFilename = string.Empty;
            MaxSize = 0;
            NativeFiles = new List<NativeFile>();
        }

        public static void LoadFile(string configFileName)
        {
            var configPath = Path.GetDirectoryName(configFileName);
            var nativeObjectFile = new NativeFile();
            nativeObjectFile.Load(configPath+"\\Object");
            NativeFiles.Add(nativeObjectFile);
            using (var fs = new FileStream(configFileName, FileMode.Open))
            {
                TextReader reader = new StreamReader(fs);
                while(reader.Peek()!=-1)
                {
                    var s = reader.ReadLine();
                    // obviously, if s is null, we do nothing.  If it starts with a #, we ignore it.
                    if (s != null && !s.StartsWith("#") && !s.StartsWith("//"))
                    {                        
                        var tokens = s.Split(new[] { ' ' });

                        if (s.Length > 0)
                        {
                            switch(tokens[0].ToLower())
                            {
                                case "maxsize":
                                    MaxSize = int.Parse(tokens[1]);
                                    break;
                                case "name":
                                    ConfigName = tokens[1];
                                    break;
                                case "native":
                                    var nf = new NativeFile();
                                    nf.Load(configPath+"\\"+tokens[1]);
                                    NativeFiles.Add(nf);
                                    break;
                                case "filename":
                                    TargetFilename = tokens[1];
                                    break;

                            }
                        }
                    }
                }
            }

        }

        internal static int GetNativeClassId(string p)
        {
            for(int i=0; i<NativeFiles.Count;i++)
            {
                for(int j=0;j<NativeFiles[i].NativeClasses.Count;j++)
                {
                    if (NativeFiles[i].NativeClasses[j].ClassName == p) return NativeFiles[i].NativeClasses[j].ClassId;
                }
            }
            return -1;
        }

        internal static int GetFieldId(string className, string name, string type)
        {
            foreach(NativeFile nf in NativeFiles)
            {
                // search through all native fields
                for (int i = 0; i < nf.NativeFields.Count; i++)
                {
                    NativeField nativeField = nf.NativeFields[i];

                    // check if we have a match
                    if ((className==nativeField.ClassName) 
                        && (name==nativeField.FieldName) 
                        && (type==nativeField.FieldType))

                        return nativeField.FieldId;
                }
            }



            return -1;
        }

        internal static int GetMethodId(string className, string name, string type)
        {
            foreach(NativeFile nf in NativeFiles)
            {
                for (int i = 0; i < nf.NativeMethods.Count; i++)
                {
                    NativeMethod nativeMethod = nf.NativeMethods[i];

                    // check if we have a match
                    if (className==nativeMethod.ClassName 
                        && name==nativeMethod.MethodName
                        && type==nativeMethod.MethodType)
                        return nativeMethod.MethodId;
                } 
            }
            // search through all native methods


            return -1;
        }

        public static int LowestNativeId
        {
            get
            {
                int lowestNativeId = int.MaxValue;
                foreach (NativeFile nf in NativeFiles)
                {
                    if (nf.LowestNativeClassId < lowestNativeId) lowestNativeId = nf.LowestNativeClassId;
                }
                return lowestNativeId;
            }
        }

        internal static void Clear()
        {
            NativeFiles.Clear();
        }

        internal static void LoadNativeFiles()
        {
            foreach(string fn in NativeFileNames)
            {
                var nf = new NativeFile();
                nf.Load(NativePath+"\\"+fn);
                NativeFiles.Add(nf);
            }
        }
    }
}
